package io.renren.modules.work.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.work.entity.ExperimentEntity;
import io.renren.modules.work.service.ExperimentService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 实验表
 *
 * @author kylin
 * @email 1360315221@qq.com
 * @date 2019-03-03 16:23:27
 */
@RestController
@RequestMapping("work/experiment")
public class ExperimentController {
    @Autowired
    private ExperimentService experimentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("work:experiment:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = experimentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("work:experiment:info")
    public R info(@PathVariable("id") Integer id){
        ExperimentEntity experiment = experimentService.getById(id);

        return R.ok().put("experiment", experiment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("work:experiment:save")
    public R save(@RequestBody ExperimentEntity experiment){
        String uuid = UUID.randomUUID().toString();
        experiment.setUuid(uuid);
        experimentService.save(experiment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("work:experiment:update")
    public R update(@RequestBody ExperimentEntity experiment){
        ValidatorUtils.validateEntity(experiment);
        experimentService.updateById(experiment);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("work:experiment:delete")
    public R delete(@RequestBody Integer[] ids){
        experimentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
